#!python3

import re

def myStrip(str, strToStrip=' '):
    searchResult = re.compile('^(' + strToStrip + ')+').search(str)
    if (None != searchResult):
        str = str[len(searchResult.group()):]
    searchResult = re.compile('(' + strToStrip + ')+$').search(str)
    if (None != searchResult):
        str = str[:0 - len(searchResult.group())]
    return str

print(myStrip('ABAB  Hello, World! AB', 'AB'))